<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 大餐计数 -->
    <script>
      var countPairs = deliciousness => {
        let res = 0
        let mod = 1000000007
        // 两数之和的最大值不超过deliciousness的最大值×2
        let MAX = Math.max(...deliciousness) * 2
        let map = new Map()
        for (let i = 0; i < deliciousness.length; i++) {
          // j = j << 1 用来乘以2
          // 从1开始找相加等于2的幂的数
          for (let j = 1; j <= MAX; j = j << 1) {
            const num = map.get(j - deliciousness[i]) || 0
            res = (res + num) % mod
          }
          // 将当前数放入map
          map.set(deliciousness[i], (map.get(deliciousness[i]) || 0) + 1)
        }
        return res
      }
      console.log(countPairs([1, 3, 5, 7, 9]))
    </script>
  </body>
</html>
